package com.fengmi.service.impl;

import com.fengmi.api.cms.CMSApi;
import com.fengmi.api.goods.GoodsApi;
import com.fengmi.cms.Content;
import com.fengmi.service.IPortalService;
import com.fengmi.vo.CatCascaderVO;
import com.fengmi.vo.CatHotGoodsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import cn.hutool.json.JSONUtil;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 作者：铁蛋
 * 时间：2021/10/14 20:29
 * 描述：永无Bug
 */
@Service
public class PortalService implements IPortalService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private GoodsApi goodsApi;

    @Autowired
    private CMSApi cmsApi;
    @Override
    public List<Content> findContentListBytpId(String tpId) {
        BoundHashOperations<String, Object, Object> operations = stringRedisTemplate.boundHashOps("menhu:portal:cms");
        Object contentListJson = operations.get(tpId);
        if (StringUtils.isEmpty(contentListJson)){
            List<Content> contentList = cmsApi.findContentListByTpid(tpId);
            operations.put(tpId,JSONUtil.toJsonStr(contentList));
            return contentList;
        }
        List<Content> contents = JSONUtil.toList((String) contentListJson, Content.class);



        return contents;
    }

    @Override
    public List<CatCascaderVO> findCatMenu() {
        BoundValueOperations<String, String> operations = stringRedisTemplate.boundValueOps("menhu:portal:catmenu");
        String catMenuJson = operations.get();
        if (StringUtils.isEmpty(catMenuJson)){
            List<CatCascaderVO> cat = goodsApi.findCat();
            operations.set(JSONUtil.toJsonStr(cat));
            return cat;
        }

        List<CatCascaderVO> catCascaderVOS = JSONUtil.toList(catMenuJson, CatCascaderVO.class);
        return catCascaderVOS;
    }

    @Override
    public List<CatHotGoodsVO> findCatAndHotGoods() {
        BoundHashOperations<String, Object, Object> operations = stringRedisTemplate.boundHashOps("menhu:portal:hot");
        List<Object> catHotGoodsVO = operations.values();

        if (StringUtils.isEmpty(catHotGoodsVO) || catHotGoodsVO.size()<=0){
            List<CatHotGoodsVO> catHotGoods = goodsApi.findCatHotGoods();
            catHotGoods.forEach(catHotGoodsVO1 -> {
                operations.put(catHotGoodsVO1.getCat().getId()+"",JSONUtil.toJsonStr(catHotGoodsVO1));
            });
            return catHotGoods;
        }
        List<CatHotGoodsVO> list = catHotGoodsVO.stream().map(json -> {
            CatHotGoodsVO catHotGoodsVO1 = JSONUtil.toBean((String) json, CatHotGoodsVO.class);

            return catHotGoodsVO1;
        }).collect(Collectors.toList());
        return list;
    }
}
